import java.util.Iterator;

public class FixedCapacityStackOfString<Item> implements Iterable<Item>{
    private Item[] a;
    private int N;
    public FixedCapacityStackOfString(int cap){
        a = (Item[]) new Object[cap];
        N = 0;
    }
    public void push(Item item){
        if(a.length==N) resize(2*a.length);
        a[N++] = item;
    }
    public Item pop(){
        Item item = a[--N];
        a[N] = null;
        if(N>0&&N==a.length) resize(a.length/2);
        return item;

    }
    public boolean isEmpty(){
        return this.N==0;
    }
    public int size(){
        return N;
    }
    public boolean isFull(){
        return N==a.length;
    }
    private void resize(int max){
        Item[] tem = (Item[]) new Object[max];
        for(int i = 0;i<a.length;i++){
            tem[i] = a[i];
        }
        a = tem;
    }
    public Iterator<Item> iterator(){
        return new ReverseArrayIterator();
    }

    private class ReverseArrayIterator implements Iterator<Item>{
        private int i = N;
        public boolean hasNext(){
            return i>0;
        }
        public Item next(){
            return a[--i];
        }
        public void remove(){

        }
    }
}


